package com.kelvin.oocl.dataprocess.view;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Scanner;
import java.util.Set;

import com.kelvin.oocl.dataprocess.entity.Customer;
import com.kelvin.oocl.dataprocess.process.DataProcess;
import com.kelvin.oocl.dataprocess.util.DateUtil;

public class Dispatcher {
	private static DataProcess dataProcess = new DataProcess();
	
	public void dataFile(File file) throws FileNotFoundException, ParseException{
		if(!file.exists()){
			throw new FileNotFoundException("Can not find the file");
		}
		Scanner scanner = new Scanner(file);
		while(scanner.hasNext()){
			String line = scanner.nextLine();
			dataProcess.process(line.trim());
		}
		scanner.close();
		
		printResult(dataProcess);
	}

	//打印结果
	private void printResult(DataProcess dataProcess2) {
		System.out.println("------------------------------------------------------------------------------");
		System.out.println("                         按id排序");
		System.out.println("------------------------------------------------------------------------------");
		Set<Customer> set = dataProcess.sortById();
		for (Customer customer : set) {
			System.out.println(customer);
		}
		System.out.println("------------------------------------------------------------------------------");
		System.out.println("                         按出生日期排序");
		System.out.println("------------------------------------------------------------------------------");
		Set<Customer> set1 = dataProcess.sortByBirthDate();
		for (Customer customer : set1) {
			System.out.println(""+customer);
		}
		System.out.println("------------------------------------------------------------------------------");
		System.out.println("                         按出id排序后以xml格式输出结果");
		System.out.println("------------------------------------------------------------------------------");
		System.out.println("<customers>");
		for (Customer customer : set) {
			System.out.println("  <customer id="+customer.getId()+">");
			System.out.println("     <name>"+customer.getName()+"</name>");
			System.out.println("     <birthDate>"+DateUtil.dateToStr(customer.getBirthDate(), "yyyy-MM-dd")+"</birthDate>");
			System.out.println("     <sex>"+(customer.isSex()==true ? "男":"女")+"</sex>");
			System.out.println("     <email>"+customer.getEmail()+"</email>");
			System.out.println("  </customer>");
		}
		System.out.println("</customers>");
	}

}
